package com.aixin.project.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aixin.project.data.*;
import com.aixin.project.handler.UserHandler;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;

import com.aixin.project.handler.CourseHandler;
import com.aixin.project.handler.ExpertHandler;
import com.aixin.project.util.DateUtil;
import com.aixin.project.util.RequestUtil;

public class OrderServlet extends BaseServlet {

    @Override
    protected Template execute(HttpServletRequest request,
                               HttpServletResponse response, Context ctx) {
        AxSession axsession = (AxSession) request.getSession().getAttribute(CommonConstants.USER_SESSION);
        if (null == axsession) {
            this.sendRedirect(response, "/");
            return null;
        }
        if (axsession.isExpert()) {
            ctx.put("error", "您不能订阅此课程！");
        } else {
            int cid = RequestUtil.getInt(request, "cid", -1);
            int eoid = RequestUtil.getInt(request, "eoid", -1);
            if (cid != -1) {
                Course course = CourseHandler.find(cid);
                if (course == null)
                    ctx.put("error", "该课程不存在");
                else if (course.getStatus().equals("已结束"))
                    ctx.put("error", "该课程已结束，请选择新的课程");
                else {
                    Expert expert = ExpertHandler.find(course.getExpertid());
                    ctx.put("expert", expert);
                    ctx.put("course", course);
                    ctx.put("eoid", eoid);
                }
            } else {
                ctx.put("error", "无效的操作");
            }
        }

        return getTemplate("order.vm");
    }

}
